//
//  MOLoadHttpsData.m
//  webIOS
//
//  Created by Canny on 2018/10/19.
//  Copyright © 2018年 WK. All rights reserved.
//

#import "MOLoadHttpsData.h"


@implementation MOLoadHttpsData

/** Post 请求 */
+(void)PostHttpDataWithUrlStr:(NSString *)url Dic:(NSDictionary *)dic SuccessBlock:(SuccessBlock)successBlock FailureBlock:(FailedBlock)failureBlock{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFJSONResponseSerializer new];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",@"text/javascript",@"text/json",@"text/plain", nil];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //    manager1.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.securityPolicy.allowInvalidCertificates = YES;
    manager.securityPolicy.validatesDomainName = NO;
    
    [manager POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"%@", responseObject);
        
        /** 这里是处理事件的回调 */
        if (successBlock) {
            successBlock(responseObject);
        }
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        /** 这里是处理事件的回调 */
        if (failureBlock) {
            failureBlock(error);
        }
        
    }];
    
    
}


+(NSString*)getCurrentTimestamp{
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[dat timeIntervalSince1970];
    NSString*timeString = [NSString stringWithFormat:@"%0.f", a*1000];//转为字符型
    return timeString;
}

/** Get 请求 */
+(void)GetHttpDataWithUrlStr:(NSString *)url Dic:(NSDictionary *)dic SuccessBlock:(SuccessBlock)successBlock FailureBlock:(FailedBlock)failureBlock{
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    

    manager.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"text/html",@"text/plain",nil];

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    
    [manager GET:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary * dic  = [MOLoadHttpsData returnDictionaryWithDataPath:responseObject];
        
        /** 这里是处理事件的回调 */
        if (successBlock) {
            successBlock(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        /** 这里是处理事件的回调 */
        if (failureBlock) {
            failureBlock(error);
        }
    }
     
     ];
    
    AFHTTPSessionManager *manager1 = [AFHTTPSessionManager manager];
    manager1.responseSerializer = [AFJSONResponseSerializer new];
    
        manager1.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",@"text/javascript",@"text/json",@"text/plain", nil];
    

    manager1.requestSerializer = [AFHTTPRequestSerializer serializer];
//    manager1.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager1.securityPolicy.allowInvalidCertificates = YES;
    manager1.securityPolicy.validatesDomainName = NO;
    
    [manager1 POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [BKIndicationView dismiss];
        NSLog(@"%@", responseObject);
        
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
    
    
}

//NSData 转字典:
// NSData转dictonary
 +(NSDictionary*)returnDictionaryWithDataPath:(NSData*)data{
         //  NSData* data = [[NSMutableData alloc]initWithContentsOfFile:path]; 拿路径文件
         NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
         NSDictionary* myDictionary = [unarchiver decodeObjectForKey:@"talkData"];
         [unarchiver finishDecoding];
         return myDictionary;
    
}
@end


